package com.hanbang.maige.view.quanzi.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hanbang.maige.R;
import com.hanbang.maige.code.MyApplication;
import com.hanbang.maige.code.base.baseadapter.ViewHolder;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.mode.javabean.quanzi.QuanZiData;
import com.hanbang.maige.utils.bitmap.GlideOptions;
import com.hanbang.maige.utils.other.SpannableUtils;
import com.hanbang.maige.utils.other.StringUtils;
import com.hanbang.maige.utils.ui.DrawableUtils;
import com.hanbang.maige.utils.ui.FocusLinkMovementMethod;
import com.hanbang.maige.utils.ui.ToastUtils;
import com.hanbang.maige.utils.ui.divider.VerticalDividerItemDecoration;
import com.hanbang.maige.widget.drawable.PingLunDrawable;
import com.hanbang.maige.widget.gridlayout.SuperGridLayout;
import com.hubang.ninegrid.NineGridView;
import com.hubang.ninegrid.NineGridViewAdapter;
import com.hubang.ninegrid.preview.ImagePreview;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 作者　　: 李坤
 * 创建时间:2016/11/1　17:31
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：
 */

public class QuanZiAdapter extends CommonAdapter<QuanZiData> {

    OnButtnClickListener onButtnClickListener;

    public QuanZiAdapter(Context context, List<QuanZiData> datas, OnButtnClickListener onButtnClickListener) {
        super(context, R.layout.item_quanzi, datas);
        this.onButtnClickListener = onButtnClickListener;
    }

    @Override
    public void convert(final ViewHolder holder, final QuanZiData quanZiData) {

        holder.setImageBitmapCircle(R.id.touxiang, quanZiData.getHeadPhotoMid(), new GlideOptions.Builder().setPlaceholder1_1());
        holder.setText(R.id.name, quanZiData.getNiChenName());
        holder.setText(R.id.time, quanZiData.getCjsj());
        holder.setText(R.id.content, quanZiData.getCONTENT());
        holder.setVisible(R.id.address, !StringUtils.isEmpty(quanZiData.address));
        holder.setText(R.id.address, quanZiData.getAddress());
        holder.setBackgroundDrawable(R.id.bottomLL, new PingLunDrawable(mContext));

        holder.setImageDrawable(R.id.dianzan, DrawableUtils.getStateSelectDrawable(R.drawable.material_dianzhan_select, R.drawable.material_dianzhan_no));
        holder.getView(R.id.dianzan).setSelected(quanZiData.isdianzan());

        holder.setVisible(R.id.dianzanTemp, quanZiData.isHaveDianzhan());
        holder.setVisible(R.id.bottomLL, quanZiData.isHaveDianzhan() || quanZiData.isHaveDianzhan());
        holder.setVisible(R.id.lineTemp, quanZiData.isHaveDianzhan() && quanZiData.isHaveHuilun());


        initDianzhanList((RecyclerView) holder.getView(R.id.dianzhanList), quanZiData);
        initHuifuData((SuperGridLayout) holder.getView(R.id.pinglunSgl), quanZiData);
        initPhoto((NineGridView) holder.getView(R.id.photosGv), quanZiData);

        holder.setOnClickListener(R.id.dianzan, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onButtnClickListener != null)
                    onButtnClickListener.onDianZhan(holder.getmPosition(), quanZiData.getID());
            }
        });


    }

    public interface OnButtnClickListener {
        void onDianZhan(int position, int quanziId);
    }


    private void initPhoto(NineGridView photosGv, final QuanZiData quanZiData) {
        if (photosGv.getAdapter() == null) {
            NineAdapter adapter = new NineAdapter(mContext, quanZiData.getFriendPhotoListSma());
            adapter.setMaxDatas(quanZiData.getFriendPhotoListMid());
            photosGv.setAdapter(adapter);
        } else {
            photosGv.setData(quanZiData.getFriendPhotoListSma());
            ((NineAdapter) photosGv.getAdapter()).setMaxDatas(quanZiData.getFriendPhotoListMid());
        }
    }

    public class NineAdapter extends NineGridViewAdapter<String> {
        ArrayList<String> maxDatas;

        public void setMaxDatas(ArrayList<String> maxDatas) {
            this.maxDatas = maxDatas;
        }

        public NineAdapter(Context context, List<String> datas) {
            super(context, datas);
        }

        @Override
        protected void onImageItemClick(Context context, NineGridView nineGridView, int index, ArrayList<String> imageDatas) {
            super.onImageItemClick(context, nineGridView, index, imageDatas);
            ImagePreview.create().images(getImageInfos(nineGridView, index, maxDatas)).selectPostion(index).start(mContext);
        }
    }

    private void initDianzhanList(RecyclerView recyclerView, QuanZiData quanZiData) {

        if (recyclerView.getLayoutManager() == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            recyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(mContext)
                    .sizeResId(R.dimen.dp_10)
                    .colorResId(R.color.translucent)
                    .build());
        }

        if (recyclerView.getAdapter() == null) {
            recyclerView.setAdapter(new DianZhanCommonAdapter(quanZiData.getDzlist()));
        } else {
            DianZhanCommonAdapter adapter = (DianZhanCommonAdapter) recyclerView.getAdapter();
            adapter.setmDatas(quanZiData.getDzlist());
            adapter.notifyDataSetChanged();
        }
    }

    private void initHuifuData(SuperGridLayout gridLayout, QuanZiData quanZiData) {
        if (gridLayout.getAdapter() == null) {
            gridLayout.setAdapter(new CommonAdapter<QuanZiData.PingLunData>(mContext, R.layout.item_quanzi_huifu, quanZiData.getPllist()) {
                @Override
                public void convert(ViewHolder holder, QuanZiData.PingLunData o) {
                    TextView pinglunIv = (TextView) holder.getConvertView();

                    pinglunIv.setMovementMethod(FocusLinkMovementMethod.getInstance());//开始响应点击事件
                    pinglunIv.setHighlightColor(mContext.getResources().getColor(R.color.gray_cc));
                    try {
                        pinglunIv.setText(SpannableUtils.getHuiFu(R.color.main_color, o.getSendContent(), o.getNiChenNameSend(), o.getNiChenNameShou()
                                , new SpannableUtils.SpanClickListener<Integer>() {
                                    @Override
                                    public void onSpanClick(Integer o) {
                                        ToastUtils.showShort(mContext, o + "");
                                        if (o == 0) {

                                        } else if (o == 1) {

                                        }
                                    }
                                }));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            });
        } else {
            CommonAdapter adapter = gridLayout.getAdapter();
            adapter.setmDatas(quanZiData.getPllist());
            adapter.notifyDataSetChanged();
        }

    }

    public class DianZhanCommonAdapter extends CommonAdapter<QuanZiData.DianZhanRen> {


        public DianZhanCommonAdapter(List<QuanZiData.DianZhanRen> datas) {
            super(QuanZiAdapter.this.mContext, R.layout.item_image, datas);
        }

        @Override
        public void convert(ViewHolder holder, QuanZiData.DianZhanRen dianZhanRen) {
            RecyclerView.LayoutParams params = new RecyclerView.LayoutParams((int) mContext.getResources().getDimension(R.dimen.dp_40), (int) mContext.getResources().getDimension(R.dimen.dp_40));
            holder.getConvertView().setLayoutParams(params);
            holder.setImageBitmap(R.id.image, dianZhanRen.getHeadPhotoSma(),
                    new GlideOptions.Builder().setPlaceholder1_1()
                            .setWidth(params.width)
                            .setHeight(params.height)
                            .addTransformation(new CropCircleTransformation(MyApplication.myApp)));
        }
    }


}
